﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Description;
using Learun.Application.Base.SystemModule;
using Learun.Application.TwoDevelopment.ZZDT_EC;
using Learun.Util;

namespace Learun.Application.Web.AppApi
{
    /// <summary>
    /// 元件库文件接口
    /// </summary>
    [RoutePrefix("api/LibraryFileApi")]
    [HandlerApiLogin(FilterMode.Enforce)]
    [TokenAuthorize]
    public class LibraryFileApiController : WebApiControllerBase
    {
        private ec_library_catalogueIBLL ec_library_catalogueIBLL = new ec_library_catalogueBLL();
        private ec_library_fileIBLL ec_library_fileIBLL = new ec_library_fileBLL();
        private AnnexesFileIBLL annexesFileIBLL = new AnnexesFileBLL();

        /// <summary>
        /// 获取元件库目录
        /// </summary>
        /// <param name="projectId">项目ID</param>
        /// <returns></returns>
        [HttpGet]
        [ResponseType(typeof(List<TreeModel>))]
        public IHttpActionResult GetTemplateCatalogue(string projectId)
        {
            try
            {
                string queryJson = "{\"ProjectId\":\"" + projectId + "\"}";
                var listCatalogue = ec_library_catalogueIBLL.GetList(queryJson);
                var listFile = ec_library_fileIBLL.GetList(queryJson);
                List<TreeModel> treeList = new List<TreeModel>();
                foreach (var itemCatalogue in listCatalogue)
                {
                    TreeModel nodeCatalogue = new TreeModel();
                    nodeCatalogue.id = itemCatalogue.LibraryCatalogueID;
                    nodeCatalogue.text = itemCatalogue.LibraryCatalogueName;
                    nodeCatalogue.value = itemCatalogue.LibraryCatalogueID;
                    nodeCatalogue.nodeType = "0";
                    nodeCatalogue.showcheck = false;
                    nodeCatalogue.checkstate = 0;
                    nodeCatalogue.isexpand = false;
                    nodeCatalogue.parentId = itemCatalogue.UpLibraryCatalogueID;
                    nodeCatalogue.NodeExtData = itemCatalogue;
                    treeList.Add(nodeCatalogue);
                    var listFileTmp = listFile.Where(x => x.LibraryCatalogueID == itemCatalogue.LibraryCatalogueID).ToList();
                    foreach (var itemFile in listFileTmp)
                    {
                        TreeModel nodeFile = new TreeModel();
                        nodeFile.id = itemFile.LibraryFileID;
                        nodeFile.text = itemFile.LibraryFileName;
                        nodeFile.value = itemFile.LibraryFileID;
                        nodeFile.nodeType = "1";
                        nodeFile.showcheck = false;
                        nodeFile.checkstate = 0;
                        nodeFile.isexpand = false;
                        nodeFile.parentId = itemCatalogue.LibraryCatalogueID;
                        nodeFile.NodeExtData = itemFile;
                        treeList.Add(nodeFile);
                    }
                }
                return Success(treeList.ToTree());
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 获取元件库文件信息
        /// </summary>
        ///  <param name="projectId">项目ID</param>
        /// <param name="libraryFileID">元件库文件ID</param>
        /// <returns></returns>
        [HttpGet]
        [ResponseType(typeof(ec_library_fileEntity))]
        public IHttpActionResult GetEntity(string projectId, string libraryFileID)
        {
            try
            {
                var entity = ec_library_fileIBLL.GetEntity(libraryFileID, projectId);
                var fileList = annexesFileIBLL.GetList(entity.FolderId).ToList();
                if (fileList != null && fileList.Count > 0)
                {
                    entity.FileId = fileList[0].F_Id;
                }
                return Success(entity);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
    }
}